{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 文件处理\n",
    "- 读文件\n",
    "- 写文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"./test.txt\",\"r+\",encoding=\"utf8\") as f:\n",
    "    line = f.readline()\n",
    "    print(line)\n",
    "    while line:\n",
    "        line = f.readline()\n",
    "        print(line)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for line in open(\"./test.txt\",\"r+\"):\n",
    "    print(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#错误的写法\n",
    "f = open(\"./test.txt\",\"r+\")\n",
    "\n",
    "try:\n",
    "    for line in f.readlines():\n",
    "        print(line)\n",
    "except Exception as e:\n",
    "    print(e)\n",
    "finally:\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = open(\"./test.txt\",\"r+\")\n",
    "type(f.readlines())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 写文件\n",
    "- \"w+\"覆盖写入文本文件\n",
    "- \"a_\"追加写入文本文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"test02.txt\",\"w+\",encoding=\"utf-8\") as f:\n",
    "    f.write(\"今天早上喝的是豆浆\")\n",
    "    f.write(\"\\n\")\n",
    "    f.flush()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"test02.txt\",\"a+\",encoding=\"utf-8\") as f:\n",
    "    f.write(\"今天早上喝的是牛奶\")\n",
    "    f.write(\"\\n\")\n",
    "    f.flush()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 遍历目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "L03_流程结构.ipynb\n",
      "L07_多线程与异常.ipynb\n",
      "L06_文件处理.ipynb\n",
      "test.txt\n",
      "L05_面向对象.ipynb\n",
      "test02.txt\n",
      "L02_标准数据类型.ipynb\n",
      "L01_标准数据类型.ipynb\n",
      "L04_函数.ipynb\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "for fname in os.listdir(\"../python基础\"):\n",
    "    print(fname)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### JSON文件处理\n",
    "\n",
    "- 读\n",
    "- 写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\"\"\"\n",
    "写json\n",
    "\"\"\"\n",
    "\n",
    "import json\n",
    "\n",
    "students = [\n",
    "    {\"name\":\"zhang3\",\"age\":32,\"sex\":\"男\"},\n",
    "    {\"name\":\"li4\",\"age\":31,\"sex\":\"女\"}\n",
    "]\n",
    "\n",
    "with open(\"students.json\",\"w+\",encoding=\"utf8\") as f:\n",
    "    json.dump(students,f,ensure_ascii=False)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "读json\n",
    "\"\"\"\n",
    "\n",
    "import json\n",
    "\n",
    "with open(\"students.json\",\"r+\",encoding=\"utf8\") as f:\n",
    "    js = json.load(f)\n",
    "    print(type(js))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
